package com.jackrain.nea.fc.cp.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.cp.api.ViewCempQueryNoCmd;
import com.jackrain.nea.fc.cp.mapper.ViewCempMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

/**
 * 店务考勤系统-营业员档案
 *
 * @author WSL
 * @create 2017/11/20
 */
@Slf4j
@Component
@Service(protocol = "dubbo" , validation = "true" , version = "1.0" , group = "cp")
public class ViewCempQueryNoCmdImpl extends CommandAdapter implements ViewCempQueryNoCmd {

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
        // 常量
        String cpcDistribId = "CP_C_DISTRIB_ID";
        String cpcStoreId = "CP_C_STORE_ID";

        ValueHolder vh = new ValueHolder();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"),"yyyy-MM-dd HH-mm-ss",SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        log.info("ViewCempQueryNoCmdImpl--param-->{}",param);

        if (null == param || param.size() == 0){
            throw new NDSException("参数格式错误！");
        }
        // Mapper
        ViewCempMapper viewCempMapper = ApplicationContextHandle.getBean(ViewCempMapper.class);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");


        // 根据param查询条件 查询【店仓档案】表
        List<HashMap> list = viewCempMapper.selectProviderApi(param.getJSONArray("IDS").toArray());
        JSONArray resultJson = new JSONArray();
        for (HashMap hashMap: list) {
            // 判断【配销中心】是否为空 CP_C_DISTRIB_ID
            if (null != hashMap.get(cpcDistribId)){
                String eCode = viewCempMapper.selStoreorgEcode(TypeUtils.castToLong(hashMap.get(cpcDistribId)));
                // 接口调用-区域
                hashMap.put("CP_C_DISTRIB_ID",eCode);
            }
            // 判断【所属店仓】是否为空 CP_C_STORE_ID
            if (null != hashMap.get(cpcStoreId)){
                String eCode = viewCempMapper.selStoreEcode(TypeUtils.castToLong(hashMap.get(cpcStoreId)));
                // 接口调用-所在店铺
                hashMap.put("CP_C_STORE_ID",eCode);
            }
            // 接口调用-性别
            if ("M".equals(hashMap.get("SEX"))){
                hashMap.put("SEX",1);
            }else{
                hashMap.put("SEX",0);
            }
            // 接口调用-入职日期
            if (null != hashMap.get("HIREDATE")) {
                hashMap.put("HIREDATE", sdf.format(TypeUtils.castToDate(hashMap.get("HIREDATE").toString())));
            }

            JSONObject jsonObject = JSON.parseObject(JSON.toJSONStringWithDateFormat(new JSONObject(hashMap),"yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            resultJson.add(jsonObject);
        }

        vh.put("data",resultJson);
        return vh;
    }
}
